package com.ruoyi.ftrl.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.ftrl.domain.ControlRecords;
import com.ruoyi.ftrl.domain.Dto.AutoSendDto;
import com.ruoyi.ftrl.domain.Dto.HandSendDto;
import com.ruoyi.ftrl.service.MessagePublisher;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ftrl.domain.ControlValve;
import com.ruoyi.ftrl.service.IControlValveService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 温控阀采集数据Controller
 * 
 * @author dihang
 * @date 2024-08-08
 */
@RestController
@RequestMapping("/ftrl/valve")
public class ControlValveController extends BaseController
{
    @Autowired
    private IControlValveService controlValveService;
    @Autowired
    private MessagePublisher messagePublisher;

    /**
     * 发送心跳机制
     * @param
     * @return
     */
    @PostMapping("/publishs")
    public String publishMessages() {
        controlValveService.heartbeat();
        return "指定频道号发布消息成功！";
    }

    /**
     * 指定频道号发布消息
     * @param message
     * @return
     */
    @PostMapping("/publish")
    public String publishMessage(@RequestParam String message) {
        messagePublisher.publish(message);
        return "指定频道号发布消息成功！";
    }

    /**
     * 户用锁闭阀开关阀门
     */
    @PostMapping("/lock")
    public AjaxResult lock(String roomNo,String state)
    {
        return toAjax(controlValveService.sendConxtrolValveLock(roomNo,state));
    }

    /**
     * 温控阀发送指令(手动调控)
     */
    @Anonymous
    @PostMapping("/send")
    public List<String> send(@RequestBody HandSendDto handSendDto) throws InterruptedException {
        return controlValveService.sendControlValve(handSendDto);
    }

    /**
     * 温控阀发送指令(自动调控)
     */
    @PostMapping("/auto")
    public AjaxResult send(@RequestBody AutoSendDto autoSendDto)
    {
        return toAjax(controlValveService.AutoSendConxtrolValve(autoSendDto));
    }

    /**
     * 接收温控阀数据
     */
    @GetMapping(value = "/collection")
    public AjaxResult collection()
    {
        return success(controlValveService.collectionControlValve());
    }


    /**
     * 查询温控阀采集数据列表
     */
    @PreAuthorize("@ss.hasPermi('ftrl:valve:list')")
    @GetMapping("/list")
    public TableDataInfo list(ControlValve controlValve)
    {
        startPage();
        List<ControlValve> list = controlValveService.selectControlValveList(controlValve);
        return getDataTable(list);
    }


    /**
     * 查询户用流量阀采集数据列表
     */
    @GetMapping("/list/flow")
    public TableDataInfo list1(ControlValve controlValve)
    {
        startPage();
        List<ControlValve> list = controlValveService.selectControlValveListFiow(controlValve);
        return getDataTable(list);
    }

    /**
     * 查询户用锁闭阀采集数据列表
     */
    @GetMapping("/list/lock")
    public TableDataInfo list2(ControlValve controlValve)
    {
        startPage();
        List<ControlValve> list = controlValveService.selectControlValveListLock(controlValve);
        return getDataTable(list);
    }

    /**
     * 查询单元阀采集数据列表
     */
    @GetMapping("/list/unit")
    public TableDataInfo list3(ControlValve controlValve)
    {
        startPage();
        List<ControlValve> list = controlValveService.selectControlValveListUnit(controlValve);
        return getDataTable(list);
    }
    /**
     * 导出温控阀采集数据列表
     */
    @PreAuthorize("@ss.hasPermi('ftrl:valve:export')")
    @Log(title = "温控阀采集数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ControlValve controlValve)
    {
        List<ControlValve> list = controlValveService.selectControlValveList(controlValve);
        ExcelUtil<ControlValve> util = new ExcelUtil<ControlValve>(ControlValve.class);
        util.exportExcel(response, list, "温控阀采集数据数据");
    }

    /**
     * 获取温控阀采集数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('ftrl:valve:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(controlValveService.selectControlValveById(id));
    }

    /**
     * 新增温控阀采集数据
     */
    @PreAuthorize("@ss.hasPermi('ftrl:valve:add')")
    @Log(title = "温控阀采集数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ControlValve controlValve)
    {
        return toAjax(controlValveService.insertControlValve(controlValve));
    }

    /**
     * 修改温控阀采集数据
     */
    @PreAuthorize("@ss.hasPermi('ftrl:valve:edit')")
    @Log(title = "温控阀采集数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ControlValve controlValve)
    {
        return toAjax(controlValveService.updateControlValve(controlValve));
    }

    /**
     * 删除温控阀采集数据
     */
    @PreAuthorize("@ss.hasPermi('ftrl:valve:remove')")
    @Log(title = "温控阀采集数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(controlValveService.deleteControlValveByIds(ids));
    }


    /**
     * 定时拉取天罡系统关于安悦佳苑的阀门数据（锁闭阀）
     * @return
     */

    @PostMapping("/lists")
    public Object lists()
    {
        Object object= controlValveService.selectControlValveLists();
        return object;
    }
    /**
     * 定时拉取天罡系统关于厚福盈的阀门数据（锁闭阀）
     * @return
     */

    @GetMapping("/listss")
    public Object listss()
    {
        Object object= controlValveService.selectControlValveListss();
        return object;
    }

    /**
     * 定时拉取天罡系统关于小察院和防疫站的阀门数据（平衡阀）
     * @return
     */

    @GetMapping("/listsss")
    public Object listsss()
    {
        Object object= controlValveService.selectControlValveListsss();
        return object;
    }
}
